package aar.test.tessocr.adapter;

import android.content.Context;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutCompat;
import android.support.v7.widget.RecyclerView;
import android.util.Pair;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import org.json.JSONObject;

import java.util.List;

import aar.test.tessocr.CabinetDetailsActivity;
import aar.test.tessocr.ListofCabinetActivity;
import aar.test.tessocr.adapter.interfaces.BaseAdapter;
import aar.test.tessocr.adapter.interfaces.OnItemClickListener;
import aar.test.tessocr.baseApplication.BaseApplication;
import aar.test.tessocr.model.CabinetListMpdel;

import aar.test.tessocr.utils.Utils;
import clx.tessocr.ocreng.R;

/**
 * Created by 149-201605 on 2016/8/1.
 */

/*
* 主界面显示 dialog的资源适配器
* */
public class CabinetListAdapter extends BaseAdapter<CabinetListAdapter.CabinetListItemHolder> {
    private List<CabinetListMpdel> mList;
    private Context context;
    private OnItemClickListener listener;
    private boolean isSimple;


    public CabinetListAdapter(Context mContext, @NonNull List<CabinetListMpdel> mList, boolean isSimple) {
        this.mList = mList;
        this.context = mContext;
        this.isSimple = isSimple;
    }

    @Override
    public CabinetListItemHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view;
        if (isSimple) {
            view = LayoutInflater.from(context).inflate(R.layout.money_list_item, parent, false);
        } else {
            view = LayoutInflater.from(context).inflate(R.layout.cabinet_list_item, parent, false);
        }

        CabinetListAdapter.CabinetListItemHolder holder = new CabinetListAdapter.CabinetListItemHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull CabinetListItemHolder holder, final int position) {
        if (isSimple) {
            holder.title.setText(mList.get(position).getCabinetname() + (mList.get(position).getIsuse().equals("1") ? "（常用）" : ""));
            holder.title_address.setText(mList.get(position).getCabinetCode());
            holder.rl_item.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (listener != null) {
                        listener.onItemClick(position);
                    }
                }
            });
        } else {
            holder.title.setText(mList.get(position).getCabinetname() + (mList.get(position).getIsuse().equals("1") ? "（常用）" : ""));
            holder.title_address.setText(mList.get(position).getAddress());
            holder.rl_item.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (listener != null) {
                        listener.onItemClick(position);
                    }
                }
            });

            for(int i=0; i<mList.get(position).getGrids().size(); i++){
                String itemId = mList.get(position).getGrids().get(i).getId();
                String itemName = mList.get(position).getGrids().get(i).getName();
                //String itemType = jsonObject1.get("type").toString();
                int count = Integer.parseInt(mList.get(position).getGrids().get(i).getCount());

                String nameId = "grid"+String.valueOf(i+1);
                //String countId = "jj2_count"+String.valueOf(i+1);
                String layoutId = "grid"+String.valueOf(i+1);
                ((TextView)holder.itemView.findViewById(Utils.getResId(nameId,R.id.class))).setText(itemName);
                //((TextView)findViewById(Utils.getResId(countId,R.id.class))).setText(itemCount);

                holder.itemView.findViewById(Utils.getResId(layoutId,R.id.class)).setLayoutParams(new LinearLayout.LayoutParams(LinearLayoutCompat.LayoutParams.WRAP_CONTENT, LinearLayoutCompat.LayoutParams.MATCH_PARENT, 1));
            }

            for(int i=0; i<mList.get(position).getGrids().size(); i++){
                String itemId = mList.get(position).getGrids().get(i).getId();
                String itemName = mList.get(position).getGrids().get(i).getName();
                //String itemType = jsonObject1.get("type").toString();
                int count = Integer.parseInt(mList.get(position).getGrids().get(i).getCount());

                String nameId = "grid"+String.valueOf(i+1)+"_quantity";
                //String countId = "jj2_count"+String.valueOf(i+1);
                String layoutId = "grid"+String.valueOf(i+1)+"_quantity";
                ((TextView)holder.itemView.findViewById(Utils.getResId(nameId,R.id.class))).setText(count+"格可用");
                //((TextView)findViewById(Utils.getResId(countId,R.id.class))).setText(itemCount);

                holder.itemView.findViewById(Utils.getResId(layoutId,R.id.class)).setLayoutParams(new LinearLayout.LayoutParams(LinearLayoutCompat.LayoutParams.WRAP_CONTENT, LinearLayoutCompat.LayoutParams.MATCH_PARENT, 1));
            }
        }

    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    class CabinetListItemHolder extends RecyclerView.ViewHolder {
        TextView title;
        TextView title_address;
        TextView title_details;
        RelativeLayout rl_item;

        public CabinetListItemHolder(View itemView) {
            super(itemView);
            if (isSimple) {
                title = itemView.findViewById(R.id.title);
                title_address = itemView.findViewById(R.id.title_time);
            } else {
                title = itemView.findViewById(R.id.title);
                title_address = itemView.findViewById(R.id.title_address);
            }

            title_details = itemView.findViewById(R.id.title_details);
            rl_item = itemView.findViewById(R.id.rl_item);
        }
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }
}


